package com.xiaohu.mayi_huiyuan.common;

import com.xiaohu.mayi_huiyuan.util.DateUtil;
import com.xiaohu.mayi_huiyuan.util.MySQLDatabaseBackup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Configurable
@EnableScheduling
public class TimedTask {

    private final static Logger logger = LoggerFactory.getLogger(Exception.class);

    @Value("${spring.datasource.username}")
    private String mysqlUserName;

    @Value("${spring.datasource.password}")
    private String mysqlPassword;

    @Value("${mysql.database.name}")
    private String dataBase;

    @Value("${mysql.backup.file.path}")
    private String filePath;

    @Autowired
    private Mail mail;

    @Value("${mail.to}")
    private String mailTo;



    @Scheduled(cron = "0 0 23 * * ?")
    @Async
    public void task() throws Exception {
        String riqi = DateUtil.geshihua(new Date());
        MySQLDatabaseBackup.exportDatabaseTool(mysqlPassword, mysqlPassword, filePath, riqi + ".sql", dataBase);
        logger.info("TimedTask......执行备份到c:/sql_backup"+riqi);
        mail.sendMail(mailTo,"数据库备份信息",riqi,riqi+".sql");
    }

}
